package com.atguigu.cloud.common.https.entity;

import com.atguigu.cloud.common.https.enums.CodeMsgEnum;
import lombok.Data;

/**
 * 统一响应结果
 */
@Data
public class Result<T> {
    private int code;
    private String message;
    private T data;
    private long timestamp;

    private Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    private Result(CodeMsgEnum codeMsgEnum, T data) {
        this(codeMsgEnum.getCode(), codeMsgEnum.getMsg(), data);
    }

    private Result(CodeMsgEnum codeMsgEnum) {
        this(codeMsgEnum, null);
    }

    /**
     * 成功响应
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(CodeMsgEnum.SUCCESS, data);
    }

    /**
     * 成功响应（无数据）
     */
    public static <T> Result<T> success() {
        return new Result<>(CodeMsgEnum.SUCCESS);
    }

    /**
     * 错误响应
     */
    public static <T> Result<T> error(CodeMsgEnum codeMsgEnum) {
        return new Result<>(codeMsgEnum);
    }

    /**
     * 错误响应（自定义消息）
     */
    public static <T> Result<T> error(CodeMsgEnum codeMsgEnum, String customMessage) {
        return new Result<>(codeMsgEnum.getCode(), customMessage, null);
    }

    /**
     * 服务器错误响应
     */
    public static <T> Result<T> err() {
        return new Result<>(CodeMsgEnum.SERVICE_ERROR);
    }
}
